#include "window.h"

namespace WinApp
{
	LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
	{
		// Check any available messages from the queue

		switch (message)
		{
		case WM_DESTROY:
			//::MessageBox (hWnd,"hh","hh",0);
			PostQuitMessage(0);
			//exit(0);

			break;
		case WM_KEYDOWN:
			if( wParam == VK_ESCAPE )
				::DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		// Always return the message to the default window
		// procedure for further processing
		return 0;
	}

	Window::Window(HINSTANCE hInstance, int width, int height)
	{
		hInst=hInstance;
		WNDCLASSEX wcex;

		// Fill in the WNDCLASSEX structure. This describes how the window
		// will look to the system
		wcex.cbSize = sizeof(WNDCLASSEX); // the size of the structure
		wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
		wcex.lpfnWndProc = (WNDPROC)WndProc; // the window procedure callback
		wcex.cbClsExtra = 0; // extra bytes to allocate for this class
		wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
		wcex.hInstance = hInstance; // handle to the application instance
		wcex.hIcon = 0; // icon to associate with the application
		wcex.hCursor = LoadCursor(NULL, IDC_ARROW);// the default cursor
		wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // the background color
		wcex.lpszMenuName  = NULL; // the resource name for the menu
		wcex.lpszClassName = "DirectXExample"; // the class name being created
		wcex.hIconSm = 0; // the handle to the small icon
		RegisterClassEx(&wcex);

		// Create the window
		wndHandle = CreateWindow(
			"DirectXExample",
			"DirectXExample",
			WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
			// the window class to use
			// the title bar text
			// the window style
			CW_USEDEFAULT, // the starting x coordinate
			CW_USEDEFAULT, // the starting y coordinate
			width, // the pixel width of the window
			height, // the pixel height of the window
			NULL,NULL,hInstance,NULL);

		// Make sure that the window handle that is created is valid
		//if (!wndHandle)
		//	return false;
		//// Display the window on the screen
		//
		//return true;
	}

	Window::~Window(void)
	{
	}
	void Window::Show()
	{
		ShowWindow(wndHandle, SW_SHOW);
		UpdateWindow(wndHandle);
	}
	HWND Window::GetWnd()
	{
		return wndHandle;
	}
	void Window::GetRect(Core::Rect* rect)
	{	
		POINT left = {0,0};
		RECT winRect;
		::GetClientRect(wndHandle,&winRect);
		::ClientToScreen(wndHandle,&left);
		rect->left = winRect.left + left.x;
		rect->top = winRect.top + left.y;
		rect->right = winRect.right + left.x;
		rect->bottom = winRect.bottom + left.y;
	}
}